package com.wt.struggle.goodbye.user.activity

import android.content.Intent
import android.os.Bundle
import android.os.Message
import android.support.design.widget.TabLayout
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.reflect.TypeToken
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.Config
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.base.Send
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.info.NewsInfo
import com.wt.struggle.goodbye.info.ProUserInfo
import com.wt.struggle.goodbye.main.activity.AddCommentActivity
import com.wt.struggle.goodbye.main.activity.CommentListActivity
import com.wt.struggle.goodbye.main.activity.ShareActivity
import com.wt.struggle.goodbye.sq.adapter.PickUserAdapter
import com.wt.struggle.goodbye.user.adapter.FansAdapter
import com.wt.struggle.goodbye.user.adapter.ManagerAdapter
import com.xin.lv.yang.utils.adapter.AdapterCallBack
import kotlinx.android.synthetic.main.coll_list_layout.*
import kotlinx.android.synthetic.main.head.*

/**
 * 收藏
 */
class CollListActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {
            Send.USER_COLLECT_CODE -> {
                removeLoadDialog()
                swipeRefreshView.isRefreshing = false
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        page++
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )

                        if (isLoad) {
                            if (arr.size != 0) {
                                addMore(arr)
                            } else {
                                showToastShort("无更多数据")
                            }
                        } else {
                            if (arr.size == 0) {
                                showToastShort("暂无数据")
                            }
                            initTieAdapter(arr)

                        }
                    }
                }
            }

            Send.GET_SHE_QU_CODE -> {
                removeLoadDialog()
                swipeRefreshView.isRefreshing = false
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        page++
                        val arr = gson!!.fromJson<ArrayList<NewsInfo>>(
                            result,
                            object : TypeToken<ArrayList<NewsInfo>>() {}.type
                        )

                        if (isLoad) {
                            if (arr.size != 0) {
                                addMoreSheQu(arr)
                            } else {
                                showToastShort("无更多数据")
                            }
                        } else {
                            if (arr.size == 0) {
                                showToastShort("暂无数据")
                            }
                            initQuanAdapter(arr)
                        }

                    }
                }
            }

            Send.GET_COLLECT_USER_CODE -> {
                removeLoadDialog()
                swipeRefreshView.isRefreshing = false
                if (netInfo.code == Config.SUCCESS) {
                    val result = netInfo.dataResult
                    if (result != null && result != "" && result != "null") {
                        page++
                        val arr = gson!!.fromJson<ArrayList<ProUserInfo>>(
                            result,
                            object : TypeToken<ArrayList<ProUserInfo>>() {}.type
                        )

                        if (isLoad) {
                            if (arr.size != 0) {
                                addMorePerson(arr)
                            } else {
                                showToastShort("无更多数据")
                            }

                        } else {
                            if (arr.size == 0) {
                                showToastShort("暂无数据")
                            }
                            initCollectUserAdapter(arr)
                        }


                    }
                }
            }
        }

    }

    private fun addMorePerson(arr: ArrayList<ProUserInfo>?) {
        moreFansList.addAll(arr!!)
        fansAdapter.update(moreFansList, object : AdapterCallBack<ProUserInfo>(fansList, moreFansList) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return oldList[oldItemPosition].id == newList[newItemPosition].id
            }

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return false
            }

            override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
                val bundle = Bundle()
                bundle.putParcelable("info", newList[newItemPosition])
                return bundle
            }

        })

    }

    private fun addMoreSheQu(arr: ArrayList<NewsInfo>?) {
        moreQuanList.addAll(arr!!)
        quanAdapter.update(moreQuanList, object : AdapterCallBack<NewsInfo>(quanList, moreQuanList) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return oldList[oldItemPosition].id == newList[newItemPosition].id
            }

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return false
            }

            override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
                val bundle = Bundle()
                bundle.putParcelable("info", newList[newItemPosition])
                return bundle
            }

        })

    }

    private fun addMore(arr: ArrayList<NewsInfo>?) {
        moreTieList.addAll(arr!!)
        tieAdapter.update(moreTieList, object : AdapterCallBack<NewsInfo>(list, moreTieList) {
            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return oldList[oldItemPosition].id == newList[newItemPosition].id
            }

            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                return false
            }

            override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
                val bundle = Bundle()
                bundle.putParcelable("info", newList[newItemPosition])
                return bundle
            }

        })

    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.coll_list_layout)
        tvTitle.text = "收藏"
        imageBack.setOnClickListener {
            finish()
        }


        collRecyclerView.layoutManager = LinearLayoutManager(this)
        collRecyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

        send!!.getCollectList(page, handler)
        showLoadDialog("获取中")

        collectTabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                page = 1
                isLoad = false

                type = when (tab!!.position) {
                    0 -> {
                        moreTieList.clear()
                        list.clear()
                        send!!.getCollectList(page, handler)
                        1
                    }
                    1 -> {
                        quanList.clear()
                        moreQuanList.clear()
                        send!!.get(page, handler)
                        2
                    }
                    2 -> {
                        fansList.clear()
                        moreFansList.clear()
                        send!!.getCollectUser(page, 0, handler)
                        3
                    }
                    else -> {
                        1
                    }
                }

                showLoadDialog("获取中")
            }
        })

        swipeRefreshView.setOnRefreshListener {
            isLoad = false
            page = 1
            when (type) {
                1 -> {
                    moreTieList.clear()
                    list.clear()
                    send!!.getCollectList(page, handler)
                }
                2 -> {
                    quanList.clear()
                    moreQuanList.clear()
                    send!!.get(page, handler)
                }
                3 -> {
                    fansList.clear()
                    moreFansList.clear()
                    send!!.getCollectUser(page, 0, handler)
                }
            }

            showLoadDialog("获取中")

        }


        swipeRefreshView.onLoadMore(collRecyclerView) {
            isLoad = true

            when (type) {
                1 -> send!!.getCollectList(page, handler)
                2 -> send!!.get(page, handler)
                3 -> send!!.getCollectUser(page, 0, handler)
            }

            showLoadDialog("获取中")

        }
    }


    var page = 1

    /**
     * 1 为帖子   2 为圈子  3 为用户
     */
    var type = 1
    /**
     * 是否加载更多
     */
    var isLoad = false

    var list = arrayListOf<NewsInfo>()
    val moreTieList = arrayListOf<NewsInfo>()

    lateinit var tieAdapter: PickUserAdapter

    lateinit var quanAdapter: ManagerAdapter
    lateinit var fansAdapter: FansAdapter

    var fansList = arrayListOf<ProUserInfo>()
    val moreFansList = arrayListOf<ProUserInfo>()

    var clickIndexPosition = -1

    /**
     * 1 为收藏  2 为点赞  3 为不喜欢  4 为评论
     */
    var clickCode = 1

    private fun initTieAdapter(arr: ArrayList<NewsInfo>) {
        this.list = arr
        this.moreTieList.addAll(arr)
        tieAdapter = PickUserAdapter(this, list)
        tieAdapter.isEnterCode = 2
        collRecyclerView.adapter = tieAdapter

        tieAdapter.onFocusOnClickListener = object : PickUserAdapter.OnFocusOnClickListener {
            override fun onFocusOn(position: Int) {
                clickCode = 6
                this@CollListActivity.clickIndexPosition = position
                send!!.attentionUser(list[position].userExtraInfo!!, handler)
            }

            override fun onCollect(position: Int) {

                clickCode = 1

                this@CollListActivity.clickIndexPosition = position
                val info=list[position]

                send!!.collect(info.id.toString(), 1, handler)
                showLoadDialog("收藏中")

            }

            override fun onZan(position: Int) {
                clickCode = 2
                this@CollListActivity.clickIndexPosition = position
                send!!.clickZan(list[position].id!!, 1, handler)
            }

            override fun onDownZan(position: Int) {
                clickCode = 3

                val intent = Intent(this@CollListActivity, SiXinOrRecoverActivity::class.java)
                intent.putExtra("code", 1)
                intent.putExtra("user", list[position].userExtraInfo)
                startActivity(intent)

//                this@CollListActivity.clickIndexPosition = position
//                send!!.unLikeZan(list[position].id!!, 1, handler)

            }

            override fun onComment(position: Int) {

                clickCode = 4
               this@CollListActivity.clickIndexPosition = position

                val intent = Intent(this@CollListActivity, CommentListActivity::class.java)
                val info=list[position]
                intent.putExtra("info", info)

                intent.putParcelableArrayListExtra("vvvvList", info.videoList)
                intent.putParcelableArrayListExtra("audioList", info.audioList)

                startActivity(intent)

            }

            override fun onMore(position: Int) {
                openShare(position)
            }

        }

    }


    /**
     * 打开分享
     */
    fun openShare(adapterPosition: Int) {
        val intent = Intent(this, ShareActivity::class.java)
        val info=list[adapterPosition]
        intent.putExtra("info", info)
        intent.putParcelableArrayListExtra("videoList",info.videoList)
        intent.putParcelableArrayListExtra("voiceList",info.audioList)
        intent.putExtra("code",2)
        startActivity(intent)

    }

    var quanList = arrayListOf<NewsInfo>()
    val moreQuanList: ArrayList<NewsInfo> = arrayListOf()

    private fun initQuanAdapter(arr: ArrayList<NewsInfo>?) {
        this.moreQuanList.addAll(arr!!)
        this.quanList = arr
        quanAdapter = ManagerAdapter(this, quanList)
        quanAdapter.code = 2
        collRecyclerView.adapter = quanAdapter

    }


    private fun initCollectUserAdapter(arr: ArrayList<ProUserInfo>) {
        this.fansList = arr
        this.moreFansList.addAll(arr)
        fansAdapter = FansAdapter(this, fansList)
        collRecyclerView.adapter = fansAdapter
    }
}